<div id="hand-driver-frame" style="height: 100%;">
    <div class="search-panel">
        <div class="search-text">
            <label>设备编号：</label>
            <input type="text" class="k-textbox input-info" data-bind="value:deviceCode" />
        </div>
        <div class="search-text">
            <label>设备名称：</label>
            <input type="text" class="k-textbox input-info" data-bind="value:deviceName" />
        </div>
        <div class="search-text">
            <label>状态：</label>
            <input id="hand-driver-status" style="width:80px;" data-bind="value:status" />
        </div>
        <div class="search-Btn">
            <button data-bind="click:search_button">查询</button>
        </div>
    </div>
    <div class="gridStyle" id="data-list" style="height: calc(100% - 65px)">

    </div>
</div>
<script>
    var HandDriver = function($parent) {
        this.$parent = $parent;
        this.sysUtils = new kendoUtils();
        this.init();
        this.findGridData();
        return this;
    }
    HandDriver.prototype = {
        init : function() {
            var bindFieldArray = this.sysUtils.getBindFieldArray(this.$parent.find(".search-panel"));
            var bindFunctionArray = [
                {fName:"search_button", fs:this.findGridData},
                {fName:"delete_button", fs:this.deleteResource},
                {fName:"edit_button", fs:this.editData},
                {fName:"create_button", fs:this.createData},
                {fName:"reset_button", fs:this.resetInfo}
            ];
            this.bindBaseModels = new bindBaseModel(null,bindFieldArray,bindFunctionArray,null);
            this.sysUtils.bindDoucment(this.bindBaseModels,this.$parent,this);
            this.createGrid();
            var statusStart = Utils.getModelValueByModelKey(BASIS.DATA_STATUS.DATA_STATUS_START);
            Utils.createDataDictDropDownList([{div:this.$parent.find("#hand-driver-status"),parentModelKey:BASIS.DATA_STATUS.ROOT,select:statusStart}],this);
        },
        /*
         * 重置按钮
         */
        resetInfo:function(e){
            var that = $(e.target).data("that");
            that.sysUtils.setBindFieldNull(that.bindBaseModels.bindFieldArray);
        },
        findGridData : /* 查询 */ function (e) {
            var _this = this;
            if (!isEmpty(e)) {
                _this = $(e.target).data("that");
            }
            var queryJson = _this.sysUtils.getBindDocJson(_this.bindBaseModels.bindFieldArray);
            var grid = _this.$parent.find("#data-list").data("kendoGrid");
            var url = baseApiPath + "hand_driver/select";
            var data = {optype:"find",json:JSON.stringify(queryJson),curdatetime:new Date().getTime()};
            var dataSource = _this.sysUtils.getDataSource({
                url : url,
                data : data,
                dataType : "odata",
                cccurdatetime : new Date().getTime()
            });
            grid.setDataSource(dataSource);
        },
        createGrid : /* 创建表格 */ function() {
            var _this = this;
            var columns = this.gridColumns();
            this.sysUtils.createGird(this.$parent.find("#data-list"), {
                dataSources : [],
                selectable : 'single',
                autoSync : true,
                reorderable : true,
                columnMenu : true,
                pageable : {input : true, numeric : false},
                columns : columns,
                dataBound:function(){
                    _this.$parent.find("#data-list").find("tr:gt(0)").unbind("dblclick");
                    _this.$parent.find("#data-list").find("tr:gt(0)").dblclick(function(e){
                        var grid = _this.$parent.find("#data-list").data("kendoGrid");
                        var dataItem = grid.dataItem(grid.select());
                        //打开查看窗口
                        _this.openResourcesWindow(dataItem["id"], false);
                    });
                }
            });
        },
        createData : function(e) {
            var _this = this;
            if (!isEmpty(e)) {
                _this = $(e.target).data("that");
            }
            _this.openResourcesWindow(undefined, true);
        },
        editData : function(e) {
            var _this = this;
            if (!isEmpty(e)) {
                _this = $(e.target).data("that");
            }
            var grid = _this.$parent.find("#data-list").data("kendoGrid");
            var dataItem = grid.dataItem(grid.select());
            if (dataItem == null) {
                notification({ title:"操作失败", message:"请选择需要修改的信息!!"},'info');
                return;
            }
            _this.openResourcesWindow(dataItem["id"], true);
        },
        gridColumns : function() {
            var heads = [
                {
                    width : 30,
                    headerTemplate : '<input type="checkbox" gridId="data-list" onclick="gridCheckAll(this)" />',
                    template : '<input type="checkbox" name="hand-driver-check" value="${id}" />'
                },
                {
                    title : '设备名称',
                    field : 'deviceName'
                },
                {
                    title : '设备编号',
                    field : 'deviceCode'
                },
                {
                    title : 'MAC地址',
                    field : 'macAddress'
                },
                {
                    title : '软件版本',
                    field : 'softwareVersion'
                },
                {
                    title : '状态',
                    template:"#:Utils.getModelNameByParentKey(BASIS.DATA_STATUS.ROOT,status)#"
                },
                {
                    title : '创建时间',
                    field : 'createTime'
                }
            ];
            return heads;
        },
        deleteResource : function(e) {
            var _this = this;
            if (!isEmpty(e)) {
                _this = $(e.target).data("that");
            }
            var resources = "";
            _this.$parent.find("input[name=\"hand-driver-check\"]:checked").each(function(inx, itm) {
                var val = $(itm).val();
                resources += val + ",";
            });
            console.log(resources);
            if ("" == resources) {
                notification({ title:"操作失败", message:"请选择至少一条需要删除的记录!!"},'info');
                return;
            }
            notification({title:"确认信息", message:"确定需要删除选中的资源信息？"}, 'confirm', {
                callback : function(){
                    resources = resources.substring(0, resources.length - 1);
                    $.ajax({
                        url : baseApiPath + "hand_driver/delete",
                        type : "post",
                        data : {"ids" : resources},
                        dataType : "json",
                        success : function(res) {
                            if (res != "") {
                                notification({ title:"删除成功", message:"删除的设备有 : {"+resources+"}"},'info');
                                _this.findGridData();
                            }
                        },
                        error : function() {
                            notification({ title:"出现异常", message:"请确保您的网络正常"}, 'info');
                        }
                    });
                }
            });
        },
        openResourcesWindow : function(id, isEdit) {
            var _this = this;
            var frameTitle = "";
            _this.editResult = null;
            _this.isEdit = false;
            var openWindow = function() {
                _this.editDiv = _this.sysUtils.openWindow(_this.editDiv, {
                    title : frameTitle,
                    height : "365px",
                    width : "700px",
                    resizable : false,
                    content : "views/parkLotCenter/facilitiesManage/handDeviceEdit.html"
                }, true);
            }
            var loadInfoById = function() {
                JsonpAjax(baseApiPath + "hand_driver/find_one/"+id, null, "json", function(res) {
                    if (res) {
                        _this.editResult = res;
                        openWindow();
                    }
                });
            }
            if (undefined == id) {
                frameTitle = "新增手持设备";
                _this.isEdit = true;
                openWindow();
            } else {
                if (isEdit == true) {
                    var grid = _this.$parent.find("#data-list").data("kendoGrid");
                    var row = grid.dataItem(grid.select());
                    if (row == null) {
                        notification({ title:"操作失败",message:"请选择至少一条需要修改的记录!!"},'info');
                        return;
                    }
                    frameTitle = "修改手持设备";
                    _this.isEdit = true;
                    loadInfoById();
                } else {
                    frameTitle = "查看手持设备详细信息";
                    _this.isEdit = false;
                    loadInfoById();
                }
            }
        }
    }

    var handDriver = new HandDriver($("#hand-driver-frame"));
</script>